package com.jack.blog.controller;

import com.jack.blog.cache.*;
import com.jack.blog.entity.Blog;
import com.jack.blog.entity.Category;
import com.jack.blog.service.BlogService;
import com.jack.blog.service.CategoryService;
import com.jack.blog.service.UserService;
import com.jack.blog.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class ClientController {
    @Value("#{config.pageSize}")
    private int PAGE_SIZE;
    @Value("#{config.host}")
    private String host;
    @Value("#{config.port}")
    private int port;
    @Autowired
    private UserServiceCache userServiceCache;
    @Autowired
    private BlogServiceCache blogServiceCache;
    @Autowired
    private CategoryServiceCache categoryServiceCache;
    @Autowired
    private NoticeServiceCache noticeServiceCache;
    @Autowired
    private MessageServiceCache messageServiceCache;


    @GetMapping("/")
    public String hello(){
        return "hello";
    }

    @GetMapping("/auth")
    public String auth(){
        return "auth";
    }

    @GetMapping("/home")
    public String home(@RequestParam(name = "page" ,defaultValue = "1",required = false)int page, Model model){
        try {
            UserVo user = userServiceCache.getUser(1);
            int uid = user.getId();
            List<BlogVo> blogs = blogServiceCache.getBlogs(uid, page);
            List<CategoryVo> categories = categoryServiceCache.getCategories(uid);
            List<NoticeVo> notices = noticeServiceCache.getNotices(uid);
            model.addAttribute("pageSize", PAGE_SIZE);
            model.addAttribute("user",user);
            model.addAttribute("blogs",blogs);
            model.addAttribute("categories",categories);
            model.addAttribute("notices",notices);
            model.addAttribute("page",page+1);
            return "home";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    @GetMapping("/user/{uid}/category/{cid}")
    public String homeByCategory(@RequestParam(name = "page" ,defaultValue = "1",required = false)int page, @PathVariable("uid")int uid, @PathVariable("cid") int cid, Model model){
        try {
            UserVo user = userServiceCache.getUser(uid);
            CategoryVo category = categoryServiceCache.getCategory(uid, cid);
            List<BlogVo> blogs = blogServiceCache.getBlogsByCid(uid, cid, page);
            model.addAttribute("user", user);
            model.addAttribute("category", category);
            model.addAttribute("blogs",blogs);
            model.addAttribute("pageSize", PAGE_SIZE);
            model.addAttribute("page",page+1);
            return "homeCategory";
        }catch (Exception e){
            e.printStackTrace();
            model.addAttribute("error","error!");
            model.addAttribute("backUrl","/home");
            return "error";
        }
    }

    @GetMapping("/user/{uid}/blog/{bid}/article")
    public String article(@PathVariable("uid")int uid, @PathVariable("bid") int bid, Model model){
        try {
            blogServiceCache.addBrowse(uid,bid);
            UserVo user = userServiceCache.getUser(uid);
            BlogVo blog = blogServiceCache.getBlog(bid);
            List<MessageVo> messages = messageServiceCache.getMessagesByBid(bid);
            model.addAttribute("user", user);
            model.addAttribute("blog",blog);
            model.addAttribute("messages",messages);
            model.addAttribute("host",host);
            model.addAttribute("port",port);
            return "article";
        }catch (Exception e){
            e.printStackTrace();
            model.addAttribute("error","blog not exists!");
            model.addAttribute("backUrl","/home");
            return "error";
        }
    }

}
